package com.onejson.ojmall.service;

import org.springframework.util.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onejson.ojmall.common.domain.PageResult;
import com.onejson.ojmall.common.domain.ResponseDTO;
import com.onejson.ojmall.common.util.OjPageUtil;
import com.onejson.ojmall.dao.SRoleDao;
import com.onejson.ojmall.model.entity.SRoleEntity;
import com.onejson.ojmall.model.form.SRoleBatchForm;
import com.onejson.ojmall.model.form.SRoleForm;
import com.onejson.ojmall.model.form.SRoleQueryForm;
import com.onejson.ojmall.model.vo.SRoleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 角色表 Service 层
 *
 * @author wxx
 */
@Service
@Slf4j
public class SRoleService extends ServiceImpl<SRoleDao, SRoleEntity> {

    @Resource
    private SRoleDao sRoleDao;

    /**
     * 获取角色表列表
     */
    public PageResult<SRoleVO> getList(SRoleQueryForm queryForm) {
        Page<?> page = OjPageUtil.convert2PageQuery(queryForm);
        List<SRoleVO> list = sRoleDao.query(page, queryForm);
        return OjPageUtil.convert2PageResult(page, list);
    }

    /**
     * 查询角色表
     */
    public SRoleVO queryById(String detailId) {

        SRoleEntity detailEntity = sRoleDao.selectById(detailId);
        SRoleVO sRoleVO = new SRoleVO();

        try {
            BeanUtils.copyProperties(sRoleVO, detailEntity);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return sRoleVO;

    }


    /**
     * 【角色表】 - 添加
     */
    public ResponseDTO<String> addSRole(SRoleForm addForm) {

        SRoleEntity entity = new SRoleEntity();

//        addForm.setId(null);

        try {
            BeanUtils.copyProperties(entity, addForm);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        sRoleDao.insert(entity);
        return ResponseDTO.ok();
    }

    /**
     * 【角色表】 - 更新
     */
    public ResponseDTO<String> updateSRole(SRoleForm updateForm) {

        SRoleEntity entity = new SRoleEntity();

        try {
            BeanUtils.copyProperties(entity, updateForm);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        sRoleDao.updateById(entity);
        return ResponseDTO.ok();
    }


    /**
     * 【角色表】- 批量更新
     */
    public ResponseDTO<String> batchUpdateSRole(SRoleBatchForm sRoleBatchForm) {

        SRoleEntity entity = new SRoleEntity();
        List<SRoleEntity> sRoleEntityList = new ArrayList<>();

        try {
            for (SRoleForm sRoleForm : sRoleBatchForm.getSRoleFormList()) {

                sRoleForm.setCreateTime(null);
                sRoleForm.setUpdateTime(null);
                entity.setRemark(sRoleBatchForm.getRemark());
                BeanUtils.copyProperties(entity, sRoleForm);
                sRoleEntityList.add(entity);
            }
        } catch (Exception e) {
            System.out.println("错误消息");
            System.out.println(e.getMessage());
        }
        this.updateBatchById(sRoleEntityList);
        return ResponseDTO.ok();
    }


    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<String> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        sRoleDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(String id) {
        if (null == id) {
            return ResponseDTO.ok();
        }

        sRoleDao.deleteById(id);
        return ResponseDTO.ok();
    }


}

